package higherfunction;

import java.util.List;
import java.util.ListIterator;
import java.util.function.Consumer;

/**
 * @Description: 高阶函数，内循环
 * @Author: tfq
 * @Date: 2025-02-10 22:14
 */
public class InnerLoop {

    public static void main(String[] args) {
        List<Integer> list = List.of(1,2,3,4,5,6,7,8,9,10);
        // 需求：逆序遍历集合，只想负责元素处理，不改变集合
        hiOrder(list, (value)->System.out.println(value));
    }

    public static <T> void hiOrder(List<T> list, Consumer<T> consumer){
        ListIterator<T> integerListIterator = list.listIterator(list.size());
        //这里不要用for循环，因为LinkedList用索引遍历会报错，只有ArrayList才支持
        while (integerListIterator.hasPrevious()){
            T integer = integerListIterator.previous();
            consumer.accept(integer);
        }
    }
}